package reader.contorller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reader.entity.Member;
import reader.entity.MemberReadState;
import reader.service.MemberService;
import reader.utils.ResponseUtils;

@RestController
@RequestMapping("/api/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @PostMapping("/register")
    public ResponseUtils registerMember(@RequestParam("name") String name, @RequestParam("password") String password, @RequestParam("code") String code, HttpServletRequest req) {
        String verifyCode = (String) req.getSession().getAttribute("capText");
        ResponseUtils response = null;
        if(!code.equalsIgnoreCase(verifyCode)){
            response = new ResponseUtils().put("error","验证码错误");
        }else {
            try{
                Member member = memberService.createMember(name, password, "nick");
                response = new ResponseUtils().put("data",member);
            }catch (Exception e){
                e.printStackTrace();
                response = new ResponseUtils().put("error",e.getMessage());
            }
        }
        return response;
    }

    @PostMapping("/login")
    public ResponseUtils loginMember(@RequestParam("name") String name, @RequestParam("password") String password,@RequestParam("code") String code, HttpServletRequest req) {
        String verifyCode = (String) req.getSession().getAttribute("capText");
        ResponseUtils response = null;
        if(!code.equalsIgnoreCase(verifyCode)){
            response = new ResponseUtils().put("error","验证码错误");
        }else {
            try {
                Member member = memberService.login(name, password);
                response = new ResponseUtils().put("data", member);
            } catch (Exception e) {
                e.printStackTrace();
                response = new ResponseUtils().put("error", e.getMessage());
            }
        }
        return response;
    }

//    查询某个人对某一本书的阅读状态
    @GetMapping("/book_State")
    public ResponseUtils bookState(@RequestParam("id") Long id,@RequestParam("bookId") Long bookId){
        ResponseUtils response = null;
        try {
            MemberReadState state = memberService.getMemberReadState(id, bookId);
            response = new ResponseUtils().put("data", state);
        } catch (Exception e) {
            e.printStackTrace();
            response = new ResponseUtils().put("error", e.getMessage());
        }
        return response;
    }

    @PostMapping("/update_readState")
    public ResponseUtils updateReadState(@RequestParam("id") Long id,@RequestParam("bookId") Long bookId,@RequestParam("readState") Integer readState){
        ResponseUtils response = null;
        try {
            MemberReadState memberReadState = memberService.updateMemberReadState(id, bookId, readState);
            response = new ResponseUtils().put("data", memberReadState);
        } catch (Exception e) {
            e.printStackTrace();
            response = new ResponseUtils().put("error", e.getMessage());
        }
        return response;
    }
}
